<template>
  <div>
    <el-form
      :model="groupForm"
      :rules="rules"
      ref="GroupForm"
      label-width="80px">
      <el-form-item
        label="群id"
        prop="groupId">
        <el-input
          placeholder="请输入群id"
          v-model="groupForm.groupId"
          disabled>
        </el-input>
      </el-form-item>
      <el-form-item
        label="群名称"
        prop="groupName">
        <el-input
          placeholder="请输入群名"
          v-model="groupForm.groupName"
          disabled>
        </el-input>
      </el-form-item>
      <el-form-item
        label="备注信息"
        prop="remarkInfo">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入备注信息"
          v-model="groupForm.remarkInfo">
        </el-input>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button @click="cancelSubmit">
        取消
      </el-button>
      <el-button type="primary" @click="submitForm('GroupForm')">
        确定
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AddGroupForm',
  props: {
    group: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      groupForm: {
        groupId: '',
        groupName: '',
        remarkInfo: '我是'
      },
      rules: {
        groupId: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        groupName: [
          { required: true, message: '请输入群组名', trigger: 'blur' }
        ]
      },
      organizationInfo: []
    }
  },
  mounted () {
    this.groupForm.groupId = this.group.groupId
    this.groupForm.groupName = this.group.groupName
    if (this.organizationId) {
      this.groupForm.organizationId = this.organizationId
    }
  },
  methods: {
    /**
     * 取消提交
     */
    cancelSubmit () {
      this.$emit('cancel')
    },
    /**
     * 提交修改表单
     */
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleApplyAddGroup()
        } else {
          return false
        }
      })
    },
    /**
     * 申请添加群
     */
    handleApplyAddGroup () {
      this.$request.contact.applyGroup(
        this.groupForm.groupId,
        this.groupForm.remarkInfo,
        res => {
          this.$emit('submit')
        },
        err => {
          this.$message.error(err.msg)
        }
      )
    }
  }
}
</script>
